
#include "ltdb.h"
#include "mem.h"
#include "lthash.h"

lt_hash_table_t*
lt_hash_create(
    schar*      name,
    uint32      n
)
{
    lt_hash_table_t*    hash_table;
    mem_obj_t*          mo;
    schar               mo_name[NAME_LEN];
    uint32              i;

    sprintf(mo_name, "%s_MO", name);
    mo = mem_obj_create(mo_name, FALSE);

    hash_table = lt_mo_alloc(mo, sizeof(lt_hash_table_t) + n * sizeof(lt_hash_cell_t) + LT_MEM_ALIGN_SIZE);
    hash_table->cell_arr = (lt_hash_cell_t *)LT_ALIGN((uint3264)(((uchar*)hash_table) + sizeof(lt_hash_table_t)), LT_MEM_ALIGN_SIZE);
    hash_table->n_cells = n;

    for (i = 0; i < n; i++)
    {
        LT_LST_INIT(hash_table->cell_arr[i].node_lst);
    }

    hash_table->mo = mo;

    lt_mutex_create(&hash_table->mutex, MUTEX_NO_LEVEL);

    return hash_table;
}

void
lt_hash_free(
    lt_hash_table_t*    hash_table             
)
{
    mutex_free(&hash_table->mutex);

    lt_mo_free(hash_table->mo, hash_table);
}


